package com.servlet;

import com.dao.UserDao;
import com.daoimpl.UserDaoImpl;
import com.vo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/InsertUserServlet")
public class InsertUserServlet extends HttpServlet {

    public  InsertUserServlet(){ super(); }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setHeader("content-type", "text/html; charset=UTF-8");
        
        User user=new User();
        user.setUsername(req.getParameter("username"));
        user.setPassword(req.getParameter("password"));
        user.setQuestion(req.getParameter("question"));
        user.setAnswer(req.getParameter("answer"));
        user.setType(req.getParameter("type"));

        UserDao dao=new UserDaoImpl();
        if(dao.findSameName(user.getUsername())){
            resp.getWriter().println("用户已存在");
        }else{
            if(dao.addUser(user)){
                req.getRequestDispatcher("login.jsp").forward(req, resp);
            }else {
                resp.getWriter().println("注册失败");
            }
        }

    }
}
